package cn.wondertek.common.utils.jwt;

import java.util.Date;

import cn.wondertek.common.constant.AuthConstant;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.auth0.jwt.interfaces.DecodedJWT;

/**
 * @author wangjunfeng
 * @date 2020/2/29 15:43
 */
public class JwtUtil {
    /**
     * 校验token是否正确
     *
     * @param token  密钥
     * @param secret 用户的密码
     * @return 是否正确
     */
    public static boolean verify(String token, String secret) {
        try {
            // 根据密码生成JWT效验器
            Algorithm algorithm = Algorithm.HMAC256(secret);
            JWTVerifier verifier = JWT.require(algorithm)
                    .withClaim(AuthConstant.USER_NAME, getUserName(token))
                    .withClaim(AuthConstant.USER_ID, getUserId(token))
                    .build();
            // 效验TOKEN
            verifier.verify(token);
            return true;
        } catch (JWTVerificationException exception) {
            return  false;
        }
    }

    /**
     * 获得token中的信息无需secret解密也能获得
     *
     * @return token中包含的用户名
     */
    public static String getUserName(String token) {
        try {
            DecodedJWT jwt = JWT.decode(token);
            return jwt.getClaim(AuthConstant.USER_NAME).asString();
        } catch (JWTDecodeException e) {
            return null;
        }
    }

    /**
     * 获得token中的信息无需secret解密也能获得
     *
     * @return token中包含的用户名
     */
    public static String getUserId(String token) {
        try {
            DecodedJWT jwt = JWT.decode(token);
            return jwt.getClaim(AuthConstant.USER_ID).asString();
        } catch (JWTDecodeException e) {
            return null;
        }
    }

    /**
     *
     * @param userName 用户名/手机号
     * @param userId   用户id
     * @param secret   用户的密码
     * @return 加密的token
     */
    public static String sign(String userName, String userId, String secret) {
        Date date = new Date(System.currentTimeMillis() + AuthConstant.ExpireTime.TWELVE_HOURS );
        Algorithm algorithm = Algorithm.HMAC256(secret);
        return JWT.create()
                .withClaim(AuthConstant.USER_NAME, userName)
                .withClaim(AuthConstant.USER_ID, String.valueOf(userId))
                .withExpiresAt(date)
                .sign(algorithm);

    }
}
